package ahblue.cn.tanzi.ui.home.article;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.ArrayList;
import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.home.ArticleCategory;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.ui.common.UIConfig;
import ahblue.cn.tanzi.util.DensityUtils;
import ahblue.cn.tanzi.view.ScaleTransitionPagerTitleView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 首页里面的文章
 */
public class HomeArticleActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTitle;
    @BindView(R.id.tl_article_page_indicator)
    MagicIndicator mTabLayoutPage;
    @BindView(R.id.vp_article_viewpager)
    ViewPager mViewPagerArticle;

    private int mType;
    private String mTypeName;
    private List<ArticleCategory> mArticleCategoryList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_article);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        mType = intent.getIntExtra("ArticleType", 0);
        mTypeName = intent.getStringExtra("ArticleTypeName");
        mTitle.setText(mTypeName);

        initData();
        initView();
    }

    private void initData() {
        /*RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getArticleCategory(mType, mArticleCategoryListcallback);*/

        mArticleCategoryList = new ArrayList<>();
        if (mType == 2){
            setTitle();
        }

        CommonNavigator commonNavigator = new CommonNavigator(HomeArticleActivity.this);
        commonNavigator.setAdapter(mNavigatorAdapter);
        mTabLayoutPage.setNavigator(commonNavigator);

        ArticleCategoryAdapter adapter = new ArticleCategoryAdapter(getSupportFragmentManager(), mArticleCategoryList);
        mViewPagerArticle.setAdapter(adapter);

        ViewPagerHelper.bind(mTabLayoutPage, mViewPagerArticle);
    }


    private void setTitle(){
        ArticleCategory ac1 = new ArticleCategory();
        ac1.setCategoryTitle("头条");
        ac1.setName("top");
        ArticleCategory ac2 = new ArticleCategory();
        ac2.setCategoryTitle("社会");
        ac2.setName("shehui");
        ArticleCategory ac3 = new ArticleCategory();
        ac3.setCategoryTitle("国内");
        ac3.setName("guonei");
        ArticleCategory ac4 = new ArticleCategory();
        ac4.setCategoryTitle("国际");
        ac4.setName("guoji");
        ArticleCategory ac5 = new ArticleCategory();
        ac5.setCategoryTitle("娱乐");
        ac5.setName("yule");
        ArticleCategory ac6 = new ArticleCategory();
        ac6.setCategoryTitle("体育");
        ac6.setName("tiyu");
        ArticleCategory ac7 = new ArticleCategory();
        ac7.setCategoryTitle("军事");
        ac7.setName("junshi");
        ArticleCategory ac8 = new ArticleCategory();
        ac8.setCategoryTitle("科技");
        ac8.setName("keji");
        ArticleCategory ac9 = new ArticleCategory();
        ac9.setCategoryTitle("财经");
        ac9.setName("caijing");
        ArticleCategory ac10 = new ArticleCategory();
        ac10.setCategoryTitle("时尚");
        ac10.setName("shishang");
        mArticleCategoryList.add(ac1);
        mArticleCategoryList.add(ac2);
        mArticleCategoryList.add(ac3);
        mArticleCategoryList.add(ac4);
        mArticleCategoryList.add(ac5);
        mArticleCategoryList.add(ac6);
        mArticleCategoryList.add(ac7);
        mArticleCategoryList.add(ac8);
        mArticleCategoryList.add(ac9);
        mArticleCategoryList.add(ac10);
    }
    private void initView() {

    }

    /**
     * 获取文章分类的回调。
     */
/*    ApiCallback<List<ArticleCategory>> mArticleCategoryListcallback = new ApiCallback<List<ArticleCategory>>() {
        @Override
        public void onSuccess(List<ArticleCategory> data) {
            mArticleCategoryList = data;

            CommonNavigator commonNavigator = new CommonNavigator(HomeArticleActivity.this);
            commonNavigator.setAdapter(mNavigatorAdapter);
            mTabLayoutPage.setNavigator(commonNavigator);

            ArticleCategoryAdapter adapter = new ArticleCategoryAdapter(getSupportFragmentManager(), data);
            mViewPagerArticle.setAdapter(adapter);

            ViewPagerHelper.bind(mTabLayoutPage, mViewPagerArticle);
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };*/

    /**
     * 单击返回按钮
     */
    @OnClick(R.id.btn_back)
    void onClickButtonBack() {
        finish();
    }

    CommonNavigatorAdapter mNavigatorAdapter = new CommonNavigatorAdapter() {

        @Override
        public int getCount() {
            return mArticleCategoryList.size();
        }

        @Override
        public IPagerTitleView getTitleView(Context context, int index) {
            ScaleTransitionPagerTitleView colorTransitionPagerTitleView = new ScaleTransitionPagerTitleView(context);
            colorTransitionPagerTitleView.setNormalColor(getResources().getColor(R.color.grey));
            colorTransitionPagerTitleView.setSelectedColor(getResources().getColor(R.color.blue_press));
            colorTransitionPagerTitleView.setTextSize(15);
            colorTransitionPagerTitleView.setText(mArticleCategoryList.get(index).getCategoryTitle());
            colorTransitionPagerTitleView.setOnClickListener(view -> mViewPagerArticle.setCurrentItem(index));

            return colorTransitionPagerTitleView;
        }

        @Override
        public IPagerIndicator getIndicator(Context context) {
            LinePagerIndicator indicator = new LinePagerIndicator(context);
            indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
            indicator.setLineHeight(DensityUtils.dp2px(context, 1.5f));
            indicator.setLineWidth(UIUtil.dip2px(context, 16));
            indicator.setYOffset(DensityUtils.dp2px(context, 3));
            indicator.setColors(getResources().getColor(R.color.blue_press));
            return indicator;
        }
    };

    /**
     * 文章分类ViewPager的adapter。
     */
    class ArticleCategoryAdapter extends FragmentPagerAdapter {

        private List<ArticleCategory> mCategoryList;

        ArticleCategoryAdapter(FragmentManager fm, List<ArticleCategory> data) {
            super(fm);

            mCategoryList = data;
        }

        @Override
        public Fragment getItem(int i) {
            ArticleCategory category = mCategoryList.get(i);

            return ArticleListFragment.newInstance(mType, "home", category.getName(),0);
        }

        @Override
        public int getCount() {
            return mCategoryList.size();
        }
    }
}
